//
//  DownloadAudio.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/20.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation
import CoreData

private let entityName = CoreDataEntityName.downloadingAudio.rawValue
private let context = CoreDataHelper.getContext()

extension DownloadingAudio {
  static func getAll() -> [DownloadingAudio]? {
    let request = NSFetchRequest<DownloadingAudio>()
    let entity = NSEntityDescription.entity(forEntityName: entityName, in: context)
    request.entity = entity
    request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: true)]
    if let currentUser = UserManager.shared.currentUser {
      request.predicate = NSPredicate(format: "userID = %@", "\(currentUser.identifier)")
    } else {
      request.predicate = NSPredicate(format: "userID = nil")
    }
    
    do {
      let downloadingAudios = try context.fetch(request)
      return downloadingAudios
    } catch let error {
      loggingPrint(error)
      return nil
    }
  }
  
  static func get(audioID: Int) -> DownloadingAudio? {
    let request = NSFetchRequest<DownloadingAudio>()
    let entity = NSEntityDescription.entity(forEntityName: entityName, in: context)
    request.entity = entity
    if let currentUser = UserManager.shared.currentUser {
      request.predicate = NSPredicate(format: "userID = %@ && audioID = %@", "\(currentUser.identifier)", "\(audioID)")
    } else {
      request.predicate = NSPredicate(format: "userID = nil && audioID = %@", "\(audioID)")
    }
    
    do {
      let downloadingAudio = try context.fetch(request).first
      return downloadingAudio
    } catch let error {
      loggingPrint(error)
      return nil
    }
  }
  
  static func add(audioID: Int, urlString: String) throws {
    do {
      if DownloadingAudio.get(audioID: audioID) == nil {
        let downloadingAudio = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context) as! DownloadingAudio
        downloadingAudio.createdAt = Date()
        downloadingAudio.url = urlString
        downloadingAudio.audioID = Int16(audioID)
        if let currentUser = UserManager.shared.currentUser {
          downloadingAudio.userID = Int16(currentUser.identifier)
        }
        
        do {
          try context.save()
        } catch let error {
          loggingPrint(error)
          throw error
        }
      }
    } catch let error {
      loggingPrint(error)
      throw error
    }
  }
  
  static func remove(audioID: Int) throws {
    do {
      if let downloadingAudio = DownloadingAudio.get(audioID: audioID) {
        context.delete(downloadingAudio)
        do {
          try context.save()
        } catch let error {
          loggingPrint(error)
          throw error
        }
      }
    } catch let error {
      loggingPrint(error)
      throw error
    }
  }
  
  static func remove(_ downloadingAudio: DownloadingAudio) throws {
    context.delete(downloadingAudio)
    do {
      try context.save()
    } catch let error {
      loggingPrint(error)
      throw error
    }
  }
}
